<template>
  <el-dialog title="提示" :visible.sync="visible" width="60%">
    <el-form :inline="true" size="mini">
      <el-row>
        <el-form-item label="编号">{{ model.tizhiId }} </el-form-item>
        <el-form-item label="学生">{{ model.tizhiStudent }} </el-form-item>
        <el-form-item label="学期">{{ model.tizhiTerm }} </el-form-item>
      </el-row>
      <el-row>
        <avue-crud :option="tableOption" :data="tableData"> </avue-crud>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="$cellEdit" @click="visible = false">取 消</el-button>
      <el-button type="primary" @click="closeDialog()">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
const fieldMap = {
  tizhiAbdominalCurl: "引体向上（男）/1分钟仰卧起坐（女）（次）",
  tizhiBodyMassIndex: "体重指数(BMI)",
  tizhiM50: "50米跑（秒）",
  tizhiM1000M800: "1000米（男）/800米（女）（分。秒）",
  tizhiSitForward: "坐位体前屈（厘米）",
  tizhiStandingJump: "立定跳远（厘米）",
  tizhiVitalCapacity: "肺活量（毫升)"
};

export default {
  props: ["handleSave", "handleUpdate"],
  data() {
    return {
      visible: false,
      model: {
        tizhiId: null,
        tizhiTerm: null,
        tizhiStudent: null,
        $cellEdit: false,
      },
      tableOption: {
        size: "mini",
        addBtn: false,
        menu: false,
        align: "center",
        columnBtn: false,
        refreshBtn: false,
        page: false,
        rowKey: "field",
        column: [
          { label: "项目", prop: "title", align: "right", width: 230 },
          {
            label: "标准成绩",
            children: [
              {
                label: "成绩",
                type: "input",
                cell: true,
                prop: "scores",
                clearable: false,
                width: 80,
              },
              {
                label: "得分",
                type: "input",
                cell: true,
                prop: "credits",
                clearable: false,
                width: 80,
              },
              {
                label: "等级",
                type: "select",
                prop: "level",
                clearable: false,
                width: 80,
              },
            ],
          },
          {
            label: "加分成绩",
            children: [
              {
                label: "成绩",
                type: "input",
                cell: true,
                prop: "scores",
                clearable: false,
                width: 80,
              },
              {
                label: "得分",
                type: "input",
                cell: true,
                prop: "credits",
                clearable: false,
                width: 80,
              },
              {
                label: "等级",
                type: "select",
                prop: "level",
                clearable: false,
                width: 80,
              },
            ],
          },
          { label: "免测说明", type: "input", cell: true, prop: "marker" },
        ],
      },
    };
  },
  computed: {
    $cellEdit() {
      return this.model.$cellEdit;
    },
    tableData() {
      return this.model2Row();
    },
  },
  created() {
    Object.keys(fieldMap).forEach((key) => {    
      this.$set(this.model, key, {
        credits: null,
        level: null,
        marker: null,
        needCredits: null,
        scores: null,
      });
    });
  },
  methods: {
    openDialog(model) {
      console.log(this.model)
      this.model = model;
      this.visible = true;
    },
    closeDialog() {
      this.row2model();
      const done = () => {
        this.visible = false;
      };
      const doNothing = () => {};
      if (this.$cellEdit === true && -1 === this.model.$index)
        this.$emit("handleSave", this.model, done, doNothing);
      else if (this.$cellEdit === true && -1 != this.model.$index)
        this.$emit(
          "handleUpdate",
          this.model,
          this.model.$index,
          done,
          doNothing
        );
      else done();
    },
    model2Row() {
      const $cellEdit = this.$cellEdit;
      return Object.keys(fieldMap)
        .map((key) => {
          return {
            ...this.model[key],
            field: key,
            title: fieldMap[key],
            $cellEdit,
          };
        });
    },
    row2model() {
      const { field, title, $cellEdit, $index } = {};
      this.tableData.forEach(
        (row) =>
          (this.model[row.field] = { ...row, field, title, $cellEdit, $index })
      );
    },
  },
};
</script>
